import express from "express";
import Result from "../../common/result.js";
import {
  UPLOAD_PATH,
  OUTPUT_PATH,
  PYTHON_PATH,
  taskList,
} from "../../const/index.js";
import path from "node:path";
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { nanoid } from "nanoid";

const pdfRouter = express.Router();

const pdfToImageScriptPath = fileURLToPath(
  new URL("../../python/pdf/pdfToImage.py", import.meta.url)
);

const pdfToDocxScriptPath = fileURLToPath(
  new URL("../../python/pdf/pdfToDocx.py", import.meta.url)
);

const pdfMergeScriptPath = fileURLToPath(
  new URL("../../python/pdf/pdfMerge.py", import.meta.url)
);

pdfRouter.post("/pdf2img", async (req, res) => {
  const { option, fileName, fileHash } = req.body;
  if (!option) {
    return res.send(Result.error(null, "参数不能为空", 500));
  }
  try {
    const pdfPath = path.join(
      UPLOAD_PATH,
      `${fileHash}${path.extname(fileName)}`
    );
    // const outputFolder = path.join(OUTPUT_PATH, fileHash);
    const process = spawn(PYTHON_PATH, [
      pdfToImageScriptPath,
      pdfPath,
      OUTPUT_PATH,
      fileHash,
      option.convertType || "page_image",
    ]);
    const taskId = nanoid();
    taskList.set(taskId, {
      fileHash,
      fileName,
      fileType: "folder",
      done: false,
    });
    process.on("close", () => {
      taskList.get(taskId).done = true;
    });
    return res.send(Result.success({ taskId }, "任务正在进行"));
  } catch (error) {
    return res.send(Result.error(error, "pdf转图片失败", 500));
  }
});

pdfRouter.post("/pdf2docx", async (req, res) => {
  const { fileName, fileHash } = req.body;
  try {
    const pdfPath = path.join(
      UPLOAD_PATH,
      `${fileHash}${path.extname(fileName)}`
    );
    const outputDocx = path.join(OUTPUT_PATH, `${fileHash}.docx`);
    const process = spawn(PYTHON_PATH, [
      pdfToDocxScriptPath,
      pdfPath,
      outputDocx,
    ]);
    const taskId = nanoid();
    taskList.set(taskId, {
      fileHash,
      fileName,
      fileType: "docx",
      done: false,
    });
    process.on("close", () => {
      taskList.get(taskId).done = true;
    });
    return res.send(Result.success({ taskId }, "任务正在进行"));
  } catch (error) {
    return res.send(Result.error(error, "pdf转docx失败", 500));
  }
});

pdfRouter.post("/merge", async (req, res) => {
  try {
    const { pdfList, fileName } = req.body;
    const taskId = nanoid();
    taskList.set(taskId, {
      fileHash: taskId,
      fileName,
      fileType: "pdf",
      done: false,
    });
    const outputPath = path.join(OUTPUT_PATH, `${taskId}.pdf`);
    const process = spawn(PYTHON_PATH, [
      pdfMergeScriptPath,
      pdfList.map((pdf) =>
        path.join(UPLOAD_PATH, `${pdf.fileHash}.${pdf.fileType}`)
      ),
      outputPath,
    ]);
    process.on("close", () => {
      taskList.get(taskId).done = true;
    });
    return res.send(Result.success({ taskId }, "任务正在进行"));
  } catch (error) {
    return res.send(Result.error(error, "pdf合并失败", 500));
  }
});

export default pdfRouter;
